package com.hl.springboothome1.controller;

import com.github.pagehelper.PageInfo;
import com.hl.springboothome1.entity.Article;
import com.hl.springboothome1.service.ArticleService;
import com.hl.springboothome1.service.Impl.ArticleServiceImpl;
import com.hl.springboothome1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
@CrossOrigin //允许跨域访问
public class ArticleController {
    @Autowired
    private ArticleService articleService = new ArticleServiceImpl();
    //添加
    @PostMapping
    public R add(@RequestBody Article article){
        int i = articleService.add(article);
        return R.ok("添加成功"+i);
    }
    //id删除
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") int id){
        int i = articleService.deleteById(id);
        return R.ok("删除"+i);
    }
    @DeleteMapping("deleteById2")
    public R deleteById2(int id){
        int i = articleService.deleteById(id);
        return R.ok("删除"+i);
    }

    //批量删除
    @RequestMapping("deleteBatch")
    public R deleteBatch(@RequestParam("list") List list){
        int i = articleService.deleteBatch(list);
        return R.ok("批量删除成功！！");
    }

    //更新
    @PutMapping
    public R update(@RequestBody Article article){
        int update = articleService.update(article);
        return R.ok("修改成功！！");
    }

    //查询
    @GetMapping("{id}")
    public R selectById(@PathVariable("id") int id){
        Article article = articleService.selectById(id);
        return R.ok("restFul模式"+article.toString());
    }
    @GetMapping("selectById2")
    public R selectById2(int id){
        Article article = articleService.selectById(id);
        return R.ok("正常拼接模式"+article.toString());
    }
    //分页动态查询
    @GetMapping("selectAll")
    public R selectAll(Article article,
                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "3") int pageSize){
        PageInfo<Article> articlePageInfo = articleService.selectAll(article, pageNum, pageSize);
        return R.ok(articlePageInfo);
    }
}
